
let wxplatform = false;
let systemInfo = null;
if (typeof wx != "undefined") {
    wxplatform = true;
    systemInfo = wx.getSystemInfoSync();
}
export const CMD = {
    setUserInfo:'setUserInfo',
    openOverView:'openOverView',
    openFriendRankView:'openFriendRankView',
    openGroupRankView:'openGroupRankView',
    startGame:'startGame',
    updateSelfRecord:'updateSelfRecord',
}

export class wxApi {
    static enable = wxplatform;
    static sysInfo = systemInfo;

    static friendRankKey: string = null;

    static shareImageUrls: Array<string> = [];
    static shareTitles: Array<string> = [];
    static shareQuery: string = null;
    static dt = 0;
    static hideTime = 0;
    static shareTicket = null;
    static start() {
        if (!wxApi.enable) return;
        let boj = wx.getLaunchOptionsSync();
        console.log("wx.getLaunchOptionsSync", boj);
        wx.onShow(function (res) {
            if (res)
                this.shareTicket = res.shareTicket;
            let currentTime = new Date().getTime();
            this.dt = currentTime - this.hideTime;
        });

        wx.onHide(function (res) {
            this.hideTime = new Date().getTime();
        });

        wx.onShareAppMessage();
        wx.setKeepScreenOn({ keepScreenOn: true });
        wx.showShareMenu({ withShareTicket: true });
    }

    static async checkSession() {
        return new Promise((resolve, reject) => {
            wx.checkSession({
                success: function () {
                    resolve();
                },
                fail: function () {
                    reject();
                },
            });
        });
    }

    static async authorize(scope: string) {
        return new Promise((resolve, reject) => {
            wx.authorize({
                scope: scope,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            })
        });
    }

    static async getSetting(): Promise<any> {
        return new Promise((resolve, reject) => {
            wx.getSetting({
                success: function (res) {
                    resolve(res);
                },
                fail: function () {
                    reject();
                }
            });
        });
    }

    static async authSettingOfUserInfo(): Promise<boolean> {
        return new Promise<boolean>(async (resolve, reject) => {
            try {
                let res = await wxApi.getSetting();
                if (res.authSetting && res.authSetting['scope.userInfo']) {
                    resolve(true);
                } else {
                    resolve(false);
                }
            } catch (e) {
                resolve(false);
            }
        });
    }
    static postMessage(cmd,data?){
        wx.postMessage(cmd,data);
    }
    static async login(): Promise<any> {
        return new Promise((resolve, reject) => {
            wx.login({
                success: function (code) {
                    resolve(code);
                },
                fail: function (...args) {
                    reject(...args);
                },
                complete: function () {
                },
            })
        });
    }

    static async shareAppMessage(title: string, imageUrl: string, query: string): Promise<any> {
        return new Promise(((resolve, reject) => {
            wx.shareAppMessage({
                title: title,
                imageUrl: imageUrl,
                query: query,
                success: function (res) {
                    console.log("=====分享成功///////////////////", res);
                    resolve(res);
                },
                fail: function (res) {
                    console.log("=====分享失败///////////////////", res);
                    reject(res);
                }
            })
        }));
    }

    static async updateShareMenu(withShareTicket: boolean): Promise<any> {
        return new Promise((resolve, reject) => {
            wx.updateShareMenu({
                withShareTicket: withShareTicket,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            })
        });
    }

    static async showShareMenu(withShareTicket: boolean): Promise<any> {
        return new Promise((resolve, reject) => {
            wx.showShareMenu({
                withShareTicket: withShareTicket,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
    }

    static backGameCommand: string = null;

    static async navigateToMiniProgram(appId: String, path: String, extraData: Object, backCommand: string, envVersion: string = "develop"): Promise<any> {
        return new Promise((resolve, reject) => {
            this.backGameCommand = backCommand;
            wx.navigateToMiniProgram({
                appId: appId,
                path: path,
                extraData: extraData,
                envVersion: envVersion,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
    }


    static async previewImage(urls: Array<string>, current?: string): Promise<any> {
        return new Promise((resolve, reject) => {
            wx.previewImage({
                urls: urls,
                current: current,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            })
        });
    }


    static async setUserCloudStorage(list) {
        return new Promise((resolve, reject) => {
            wx.setUserCloudStorage({
                KVDataList: list,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    console.log(res, "setUserCloudStorage fail");
                    reject(res);
                }
            });
        });
    }
    static async loadGroupCloudStorage(...args) {
        return new Promise((resolve, reject) => {
            if (wxApi.enable) {
                wx.postMessage({ command: "LoadGroupCloudStorageCommand", data: { keyList: [wxApi.friendRankKey], shareTicket: args[0] } });
            }
            resolve();
        });
    }
    static async loadFriendCloudStorage() {
        return new Promise((resolve, reject) => {
            wx.postMessage({ command: "LoadFriendCloudStorageCommand", data: [wxApi.friendRankKey] });
            resolve();
        });
    }
    static async setMyScoreCloudStorage(key: string, score: number) {
        let list = [];
        list.push({
            key: key,
            value: JSON.stringify({
                wxgame: { score: score, update_time: Date.now() }
            })
        });
        return this.setUserCloudStorage(list);
    }
    static async updateMyScore(...args) {
        return new Promise((resolve, reject) => {
            if (wxApi.enable) {
                wx.postMessage({ command: "UpdateMyScore", data: args[0] });
            }
            resolve();
        });
    }
    static async showFriendsRank() {
        return new Promise((resolve, reject) => {
            if (wxApi.enable) {
                wx.postMessage({ command: "ShowFriendsRank" });
            }
            resolve();
        });
    }
    static async showGroupRank() {
        return new Promise((resolve, reject) => {
            if (wxApi.enable) {
                wx.postMessage({ command: "ShowGroupRank", });
            }
            resolve();
        });
    }
    static async showOver() {
        return new Promise((resolve, reject) => {
            if (wxApi.enable) {
                wx.postMessage({ command: "ShowOver" });
            }
            resolve();
        });
    }
}
